﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ConjureBivouac2012.GameStates
{
    public class MainMenu : IGlobalGameState
    {
        private Texture2D _background;

        private int nbMenus = 2;
        private int focusPosition = 1;
        private bool EnterHit = false;
        private Dictionary<int, Texture2D> menuItems = new Dictionary<int, Texture2D>();
        private ContentManager _contentManager = null;
        private GraphicsDevice _device;

        private Texture2D _selectButton;
        private int MenuHeight = 60;
        private Vector2 startPos = new Vector2();

        private GamePadState _previousGamePadState;
        private KeyboardState _previousKeyboardState;

        public MainMenu(ContentManager contentManager, GraphicsDevice device)
        {
            
            _contentManager = contentManager;
            _device = device;

            _previousGamePadState = GamePad.GetState(PlayerIndex.One);
            _previousKeyboardState = Keyboard.GetState();

            _background = contentManager.Load<Texture2D>("SplashScreen");
        }

        public void Enter()
        {
            EnterHit = false;
            focusPosition = 1;
            startPos = new Vector2((float)_device.Viewport.Width / 2 - 50, (float)_device.Viewport.Height / 2 + (float)_device.Viewport.Height / 10);

            //Initialise Menu Icons
            menuItems.Add(1, _contentManager.Load<Texture2D>("NewGame"));
            menuItems.Add(2, _contentManager.Load<Texture2D>("Credits"));

            _selectButton = _contentManager.Load<Texture2D>("selectButton");
        }

        public void Execute(GameTime gameTime)
        {
            
            if(EnterHit)
            {
                switch(focusPosition)
                {
                    case 1:
                        Program.GetGameInstance.SetState(new PlayGame(_contentManager, _device));
                        break;

                    case 2:
                        Program.GetGameInstance.SetState(new GameCredits(_contentManager, _device));
                        break;
                }
            }
            else
            {
                GamePadState gamePad = GamePad.GetState(PlayerIndex.One);
                KeyboardState keyboard = Keyboard.GetState();

                if ((keyboard.IsKeyDown(Keys.Enter) && !_previousKeyboardState.IsKeyDown(Keys.Enter)) || (gamePad.Buttons.Start == ButtonState.Pressed && _previousGamePadState.Buttons.Start == ButtonState.Released) ||
                    (gamePad.Buttons.A == ButtonState.Pressed && _previousGamePadState.Buttons.A == ButtonState.Released))
                {
                    EnterHit = true;
                }

                if (((gamePad.DPad.Up == ButtonState.Pressed && _previousGamePadState.DPad.Up == ButtonState.Released) || (keyboard.IsKeyDown(Keys.Up) && !_previousKeyboardState.IsKeyDown(Keys.Up)) ||
                    gamePad.ThumbSticks.Left.Y >= 0.8) && focusPosition > 1)
                {
                    focusPosition--;
                }

                if (((gamePad.DPad.Down == ButtonState.Pressed && _previousGamePadState.DPad.Down == ButtonState.Released) || (keyboard.IsKeyDown(Keys.Down) && !_previousKeyboardState.IsKeyDown(Keys.Down)) ||
                    gamePad.ThumbSticks.Left.Y <= -0.8) && focusPosition < nbMenus)
                {
                    focusPosition++;
                }
            }

            _previousGamePadState = GamePad.GetState(PlayerIndex.One);
            _previousKeyboardState = Keyboard.GetState();
        }

        public void Exit()
        {
            
        }

        public void Draw(SpriteBatch theSpriteBatch)
        {
            foreach (int i in menuItems.Keys)
            {
                if (menuItems[i] != null)
                {
                    Vector2 pos = startPos + new Vector2(0, (i - 1)*MenuHeight);
                    float scale = (float) MenuHeight/menuItems[i].Height;
                    theSpriteBatch.Draw(menuItems[i], pos, null, Color.White, 0, new Vector2(0, 0),
                                        scale, SpriteEffects.None, 0);

                    if(focusPosition == i && _selectButton != null)
                    {
                        float scaleSelect = ((float)MenuHeight / 2) / _selectButton.Height;
                        Vector2 offset = new Vector2(-((float)_selectButton.Width / 2 * scale + 20), ((float)_selectButton.Height / 2) * scaleSelect);
                        theSpriteBatch.Draw(_selectButton, pos + offset, null, Color.White, 0, new Vector2(0, 0),
                                        scaleSelect, SpriteEffects.None, 0);
                    }
                }
            }

            theSpriteBatch.Draw(_background, new Rectangle(0,0,_device.Viewport.Width,_device.Viewport.Height), null, Color.White, 0, new Vector2(0, 0),
                                        SpriteEffects.None, 1);
        }
    }
}
